package com.luocj.muke.demo.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.luocj.muke.demo.R;
import com.luocj.muke.demo.adapter.ShopcartAdapter;
import com.luocj.muke.demo.interIml.OnAddClickedListener;
import com.luocj.muke.demo.interIml.OnMinusClickedListener;
import com.luocj.muke.demo.model.ShopCartModel;

import java.util.ArrayList;
import java.util.List;

public class ShopcartActivity extends AppCompatActivity {

    private static final String TAG = ShopcartActivity.class.getSimpleName();
    private int selectedAllTag = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopcart);
        intiView();
    }

    private void intiView() {
        RecyclerView recyclerview = findViewById(R.id.recyclerview);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerview.setLayoutManager(layoutManager);
        final ShopcartAdapter adapter = new ShopcartAdapter(ShopcartActivity.this, initData());
        recyclerview.setAdapter(adapter);
        final ImageView selectedAll = findViewById(R.id.iv_selected_all);

        selectedAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null == v.getTag() || (int) v.getTag() == 0) {
                    v.setTag(1);//全选
                    selectedAll.setImageResource(R.mipmap.ssdk_oks_classic_check_checked);
                } else {
                    v.setTag(0);//反选
                    selectedAll.setImageResource(R.mipmap.ssdk_oks_classic_check_default);
                }

                int tag = (int) v.getTag();
                int size = adapter.getmDatas().size();
                for (int i = 0; i < size; i++) {
                    if (tag == 1) {
                        adapter.getmDatas().get(i).setChecked(true);
                    } else {
                        adapter.getmDatas().get(i).setChecked(false);
                    }
                }

                adapter.notifyDataSetChanged();
            }
        });

        adapter.setOnAddClickedListener(new OnAddClickedListener() {
            @Override
            public void OnAddClicked(int position) {
                List<ShopCartModel> shopCartModels = adapter.getmDatas();
                int num = shopCartModels.get(position).getNum();
                shopCartModels.get(position).setNum(num+1);
                adapter.setmDatas(shopCartModels);
                adapter.notifyItemRangeChanged(position,1);
            }
        });
        adapter.setOnMinusClickedListener(new OnMinusClickedListener() {
            @Override
            public void OnMinusClicked(int position) {
                List<ShopCartModel> shopCartModels = adapter.getmDatas();
                int num = shopCartModels.get(position).getNum();
                shopCartModels.get(position).setNum(num -1);
                adapter.setmDatas(shopCartModels);
                adapter.notifyItemRangeChanged(position,1);
            }
        });

        adapter.setOnClearBtnClick(new ShopcartAdapter.onClearBtnClick() {
            @Override
            public void onClearClickListener(int position) {
                List<ShopCartModel> shopCartModels = adapter.getmDatas();
                shopCartModels.remove(position);
                adapter.setmDatas(shopCartModels);
                adapter.notifyDataSetChanged();
            }
        });

        adapter.setOnItemSelected(new ShopcartAdapter.OnItemSelected() {
            @Override
            public void OnItemSelected(int position, boolean isSelected) {
                Log.i(TAG, "OnItemSelected: ");
                List<ShopCartModel> shopCartModels = adapter.getmDatas();
                shopCartModels.get(position).setChecked(isSelected);
                adapter.setmDatas(shopCartModels);
                adapter.notifyDataSetChanged();

            }
        });

    }

    private ArrayList<ShopCartModel> initData() {
        ArrayList<ShopCartModel> shopCartModels = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            shopCartModels.add(new ShopCartModel("item :" + i, i, false, i));
        }
        return shopCartModels;
    }
}
